/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DesignArea.java
 *
 * Created on 2008-12-18, 15:23:18
 */
package impss;

import java.sql.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.text.*;

/**
 *
 * @author Mikehood
 */
public class DesignArea extends javax.swing.JFrame {

    int id, selectedRow, pid, aid, paid, selectedAID, areaLine;
    int[] aidToInsert;
    Object[][] a;
    Object[][] b;
    Object[] c;
    ResultSet rs;
    DBAdapter adapter;

    public void setID(int i) {
        id = i;
    }

    /** Creates new form DesignArea */
    public DesignArea() {
        initComponents();
        adapter = new DBAdapter();
        setAcTable();
        jLabel7.setVisible(false);
        aidToInsert = new int[10];
        paid = 0;
    }

    public void setDesignTable() {
        List<Phone> list = new ArrayList<Phone>();
        try {
            String sql = "select * from phone where(designState='0' and designerID='" + id + "');";
            rs = adapter.executeQuery(sql);
            while (rs.next()) {
                Phone ac = new Phone();
                ac.setPhoneID(rs.getInt("PhoneID"));
                ac.setPhoneName(rs.getString("phoneName"));
                ac.setDesignGoal(rs.getString("designGoal"));
                list.add(ac);
            }
        } catch (SQLException e) {
        }
        int size = list.size();
        a = new Object[size][3];
        for (int i = 0; i < size; i++) {
            a[i][0] = list.get(i).getPhoneID();
            a[i][1] = list.get(i).getPhoneName();
            a[i][2] = list.get(i).getDesignGoal();
        }
        jTable1.setModel(new javax.swing.table.DefaultTableModel(a, new String[]{"型号", "名称", "设计要求"}));
    }

    private void setAcTable() {
        List<Accessory> list2 = new ArrayList<Accessory>();
        List<Af> list3 = new ArrayList<Af>();
        try {
            String sql = "select * from accessory;";
            rs = adapter.executeQuery(sql);
            while (rs.next()) {
                Accessory ac = new Accessory();
                ac.setAccessoryID(rs.getInt("accessoryID"));
                ac.setAfID(rs.getInt("afID"));
                ac.setAccessoryType(rs.getString("accessoryType"));
                ac.setAccessoryStandard(rs.getString("accessoryStandard"));
                ac.setAccessoryInfo(rs.getString("accessoryInfo"));
                ac.setAccessoryPrice(rs.getInt("accessoryPrice"));
                list2.add(ac);
            }
        } catch (SQLException e) {
        }
        int size = list2.size();
        b = new Object[size][6];
        c = new Object[size];
        for (int i = 0; i < size; i++) {
            b[i][0] = list2.get(i).getAccessoryID();
            c[i] = list2.get(i).getAfID();
            b[i][2] = list2.get(i).getAccessoryType();
            b[i][3] = list2.get(i).getAccessoryStandard();
            b[i][4] = list2.get(i).getAccessoryInfo();
            b[i][5] = list2.get(i).getAccessoryPrice();
        }
        try {
            String sql = "";
            for (int i = 0; i < size; i++) {
                sql = "select afName from af where(afID='" + c[i] + "');";
                rs = adapter.executeQuery(sql);
                while (rs.next()) {
                    Af af = new Af();
                    af.setAfName(rs.getString(1));
                    list3.add(af);
                }
            }
        } catch (SQLException e) {
        }
        for (int i = 0; i < size; i++) {
            b[i][1] = list3.get(i).getAfName();
        }
        jTable2.setModel(new javax.swing.table.DefaultTableModel(b, new String[]{"配件型号", "制造商", "类型", "规格", "介绍", "报价"}));
    }

    private int submit() {
        int n = jTextArea1.getLineCount() - 1;
        int[] acid = new int[n];
        for (int i = 0; i < n; i++) {
            try {
                acid[i] = Integer.valueOf(jTextArea1.getText(i * 6, 5));
            } catch (BadLocationException ex) {
            } catch (NumberFormatException ex) {
            }
        }
        String sql = "update phone set phoneColor='" + jTextField1.getText() +
                "',phoneMaterial='" + jComboBox2.getSelectedItem().toString() +
                "',phoneView='" + jComboBox3.getSelectedItem().toString() +
                "',designState='1',phoneSum='0' where(phoneID='" + pid + "');";
        adapter.execute(sql);
        for (int i = 0; i < n; i++) {
            sql = "insert into structure values('" + pid + "','" + acid[i] + "');";
            adapter.execute(sql);
            if (!adapter.isSuccessful()) {
                return 1;
            }
        }
        return 0;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setTitle("设计");

        jButton3.setText("提交");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "型号", "类别", "规格", "介绍", "报价", "制造商"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable2);

        jTextArea1.setColumns(6);
        jTextArea1.setRows(5);
        jTextArea1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextArea1MouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(jTextArea1);

        jButton2.setText("<—");
        jButton2.setToolTipText("从列表中删除");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("—>");
        jButton1.setToolTipText("添加到列表");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel6.setText("硬件设计：");
        jLabel6.setToolTipText("从表格中选择配件并添加到列表");

        jTextField1.setText("白");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "金属", "塑料", "混合" }));

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "直板", "翻盖", "滑盖", "全触摸", "变形金刚" }));

        jLabel5.setText("样式：");

        jLabel4.setText("材质：");

        jLabel3.setText("颜色：");

        jLabel2.setText("外形设计：");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "型号", "名称", "设计要求"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel7.setText("jLabel7");

        jLabel1.setText("待设计的手机：");

        jLabel8.setText("当前选择的配件型号：");

        jLabel9.setText("无");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(66, 66, 66)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(85, 85, 85)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel2)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2)
                            .addComponent(jButton1))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(218, 218, 218)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(71, 71, 71)
                        .addComponent(jButton1)
                        .addGap(29, 29, 29)
                        .addComponent(jButton2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane3)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addComponent(jButton3))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 517, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        int re = submit();
        if (0 == re) {
            JOptionPane.showMessageDialog(this, "成功提交设计方案！");
            jLabel7.setText("");
            setDesignTable();
        } else if (1 == re) {
            JOptionPane.showMessageDialog(this, "提交设计方案时发生错误！");
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        selectedRow = jTable1.getSelectedRow();
        pid = a[selectedRow][0].hashCode();
        jLabel7.setText("" + pid);
        jLabel7.setVisible(true);
    }//GEN-LAST:event_jTable1MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (0 == aid) {
            return;
        }
        try {
            for (int i = 0; i < jTextArea1.getLineCount() - 1; i++) {
                String str = jTextArea1.getText(i * 6, 5);
                if (str.equals(aid + "")) {
                    return;
                }
            }
            jTextArea1.append(aid + "\n");
        } catch (BadLocationException ex) {
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        int start = 6 * areaLine;
        int end = 6 + start;
        try {
            jTextArea1.replaceRange("", start, end);
        } catch (IllegalArgumentException e) {
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
        // TODO add your handling code here:
        int row = jTable2.getSelectedRow();
        aid = b[row][0].hashCode();
        jLabel9.setText("" + aid);
    }//GEN-LAST:event_jTable2MouseClicked

    private void jTextArea1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextArea1MouseClicked
        // TODO add your handling code here:
        try {
            int line = jTextArea1.getSelectionEnd() / 6;
            areaLine = line;
            String str = "";
            str = jTextArea1.getText(6 * line, 5);
            selectedAID = Integer.valueOf(str);
        } catch (BadLocationException ex) {
        } catch (NumberFormatException ex) {
        }
    }//GEN-LAST:event_jTextArea1MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DesignArea().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
